package com.sas.web.dataProvider;

import java.util.Iterator;
import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.IModel;

import com.sas.model.EntityModel;
import com.sas.repository.FlowRepository;
import com.sas.web.panel.articles.ArticlesListPanel;

@SuppressWarnings("serial")
public class SortableFlowDataProvider<Flow> extends SortableDataProvider<Flow>{
	private FlowRepository flowRepository;
	private ArticlesListPanel alp; 

	public SortableFlowDataProvider(FlowRepository flowRpository,
			String defaultProperty, ArticlesListPanel alp) {
		this.flowRepository = flowRpository;
		this.alp = alp;
		setSort(defaultProperty, true);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public Iterator<? extends Flow> iterator(int first, int count) {
		SortParam sp = getSort();
		List<Flow> ret = (List<Flow>) flowRepository.findByArticle(alp.getSelectedArticle(), sp.getProperty(), sp.isAscending()).subList(first, first + count);
		return ret == null ? null : ret.iterator();
	}

	@Override
	public IModel<Flow> model(Flow object) {
		return new EntityModel<Flow>(object.getClass(), object);
	}

	@SuppressWarnings("unchecked")
	@Override
	public int size() {
		SortParam sp = getSort();
		List<Flow> ret = (List<Flow>) flowRepository.findByArticle(alp.getSelectedArticle(), sp.getProperty(), sp.isAscending());
		return ret == null ? 0 : ret.size();
	}

}
